package com.dg.acb.utils.xml;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.xmlpull.v1.XmlSerializer;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Xml;
/**
 *  XML写入
 * @author hjwang2
 * @version 1.0
 * @since 2015-8-20
 */
public class XmlSerializerCtrl implements IXmlCtrl{
    private XmlSerializer mXmlSerializer;
    private BufferedOutputStream mOutStream;
    private final String mXmlFilePath;
    private final String mHomeTag;
    public XmlSerializerCtrl(String xmlFilePath, String homeTag){
        this.mXmlFilePath = xmlFilePath;
        this.mHomeTag  = homeTag;
    }

    public boolean startSerializer() {
        if (mXmlSerializer != null || mOutStream != null) {
            throw new IllegalArgumentException("有正在进行的操作未finish");
        }
        //由android.util.Xml创建一个XmlSerializer实例
        mXmlSerializer = Xml.newSerializer();
        try {
            File outFile = new File(mXmlFilePath);
            if (!outFile.getParentFile().isDirectory()) {
                if (outFile.getParentFile().mkdirs()) {
                    throw new IOException("mkdirs error:" + outFile.getParentFile());
                }
            }
            mOutStream = new BufferedOutputStream(new FileOutputStream(outFile));
            //设置输出方向为out
            mXmlSerializer.setOutput(mOutStream, ENCODING);
            mXmlSerializer.startDocument(ENCODING, true);
            mXmlSerializer.startTag(NAME_SPACE, mHomeTag);
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean finishSerializer(){
        if (mXmlSerializer == null || mOutStream == null) {
            throw new IllegalArgumentException("finish 之前需要先start");
        }
        try {
            mXmlSerializer.endTag(NAME_SPACE, mHomeTag);
            mXmlSerializer.endDocument();
            mOutStream.flush();
            mOutStream.close();
            mXmlSerializer = null;
            mOutStream = null;
            return true;
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public void writeTag(XmlTag tag) throws IOException{
        mXmlSerializer.startTag(NAME_SPACE, tag.getName());
        Bundle attributes = tag.getAttributes();
        for (String key : attributes.keySet()) {
            mXmlSerializer.attribute(NAME_SPACE, key,
                    attributes.get(key).toString());
        }
        if (tag.hasSubTags()) {
            List<XmlTag> subTags = tag.getSubTags();
            for (XmlTag subTag : subTags) {
                writeTag(subTag);
            }
        }else {
            String value = tag.getValue();
            if(!TextUtils.isEmpty(value)){
                mXmlSerializer.text(value);
            }
        }
        mXmlSerializer.endTag(NAME_SPACE, tag.getName());
    }
}